// 配置医生相关接口
const express = require('express')
const router = express.Router();
const Response = require('../utils/Response.js')

// 引入mysql连接池
const pool = require('../utils/db.js');
const { error } = require('../utils/Response.js');

// 通过医院查询医生信息
router.post('/hospital_doctor',(req,res) =>{
  let {hospital_id} = req.body
  let sql = 'select * from hospital_doctor where hospital_id=?'
  pool.query(sql,[hospital_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 分页查询所有医生信息
router.get("/docotr-all/limit", async (req, resp) => {
  // 获取请求参数   get请求的参数封装在req.query中
  let { page, pagesize } = req.query;
  // 执行查询数组业务
  try {
    let startIndex = (page - 1) * pagesize;
    let size = parseInt(pagesize);
    let sql = "select * from hospital_doctor limit ?,?";
    let result = await pool.querySync(sql, [startIndex, size]);
    // 执行查询总条目数
    let sql2 = "select count(*) as count from hospital_doctor";
    let result2 = await pool.querySync(sql2, [startIndex, size]);
    let total = result2[0].count;
    resp.send(
      Response.ok({ page: parseInt(page), pagesize: size, total, result })
    );
  } catch (error) {
    resp.send(Response.error(error));
  }
});

// 根据医生id查询该医生收到的所有评论
router.post('/doctor-rate',(req,res) =>{
  let {doctor_id} = req.body
  let sql = 'select rate_content from rate where doctor_id=?'
  pool.query(sql,[doctor_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 通过医生擅长内容关键字模糊查询医生列表
router.post('/doctorLike',(req,res) =>{
  let {doctor_great} = req.body
  let sql = `select * from hospital_doctor where doctor_great like '%${doctor_great}%'`
  pool.query(sql,[doctor_great],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 根据医生姓名查找医生信息
router.post('/doctorByname',(req,res) =>{
  let {doctor_name} = req.body
  let sql = 'select * from hospital_doctor where doctor_name=?'
  pool.query(sql,[doctor_name],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 根据医生id查询该医生详细信息
router.post('/doctorById',(req,res)=>{
  let {doctor_id} = req.body
  let sql = 'select * from hospital_doctor where doctor_id=?'
  pool.query(sql,[doctor_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 通过科室查询医生信息
router.post('/hospital_doctorByd_id',(req,res) =>{
  let {department_id} = req.body
  let sql = 'select * from hospital_doctor where department_id=?'
  pool.query(sql,[department_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 新增医生
router.post('/hospital_doctor/add',(req,res) =>{
  let {hospital_id,hospital_name,doctor_avatar,doctor_name,doctor_sex,doctor_titles,doctor_great,doctor_isonline,department_id,doctor_grade,doctor_id,department_name} = req.body
  let sql = 'insert into hospital_doctor (hospital_id,hospital_name,doctor_avatar,doctor_name,doctor_sex,doctor_titles,doctor_great,doctor_isonline,department_id,doctor_grade,doctor_id,department_name) values (?,?,?,?,?,?,?,?,?,?,?,?)'
  pool.query(sql,[hospital_id,hospital_name,doctor_avatar,doctor_name,doctor_sex,doctor_titles,doctor_great,doctor_isonline,department_id,doctor_id,doctor_grade,department_name],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 删除医生
router.delete('/hospital_doctor/delete',(req,res) =>{
  let {doctor_id} = req.body
  let sql = 'delete from hospital_doctor where doctor_id=?'
  pool.query(sql,[doctor_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw error
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 修改医生信息
router.post('/hospital_Updatedoctor',(req,res)=>{
  let {hospital_id,hospital_name,doctor_avatar,doctor_name,doctor_sex,doctor_titles,doctor_great,doctor_isonline,department_id,doctor_grade,department_name,doctor_id} = req.body
  let sql = 'update hospital_doctor set hospital_id=?,hospital_name=?,doctor_avatar=?,doctor_name=?,doctor_sex=?,doctor_titles=?,doctor_great=?,doctor_isonline=?,department_id=?,doctor_grade=?,department_name=? where doctor_id=?'
  pool.query(sql,[hospital_id,hospital_name,doctor_avatar,doctor_name,doctor_sex,doctor_titles,doctor_great,doctor_isonline,department_id,doctor_grade,department_name,doctor_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 设置医生是否在线
router.post('/doctor_isonline',(req,res) =>{
  let {doctor_isonline,doctor_id} = req.body
  let sql = 'update hospital_doctor set doctor_isonline=? where doctor_id=?'
  pool.query(sql,[doctor_isonline,doctor_id],(err,result) =>{
    if(err){
      
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

module.exports = router